package admin.controllers;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import search.ArticleSearchEngine;

import entities.Article;

import admin.models.ArticleModel;


public class DeleteArticleServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		HttpSession session = request.getSession();
		response.setContentType("text/html");
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		try {
			ArticleModel articleModel = ArticleModel.getInstance();
			Article article = articleModel.getArticle(id);
			if (article != null) {
				articleModel.delete(article);
				
				//Set search article
				ArticleSearchEngine.remove(id);
				
				
			}
			
			session.setAttribute("success", "Delete success article " + id);
		} catch (Exception e) {
			// TODO: handle exception
		}
		response.sendRedirect("/admin/article/search");
	}
}
